<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item label="">
         <el-input style="width:300px;" class="filter-item" placeholder="请输入角色名称" v-model="listQuery.roleName"></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-button class="filter-item" type="primary" icon="el-icon-search" @click="getList()">搜索</el-button>
        </el-form-item>
        <el-form-item label="">
          <el-button class="filter-item" type="primary" @click="handleCreate()" icon="el-icon-plus">新增</el-button>
        </el-form-item>
     </el-form>
    </div>
    <el-table :data="list" v-loading="listLoading" element-loading-text="数据正在加载" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="角色编号" prop="code"/>
      <el-table-column align="center" label="角色名称" prop="name"/>
      <el-table-column align="center" label="操作" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="handleCreate(scope.row.id)">编辑</el-button>
          <el-button v-if="scope.row.status!='deleted'" size="mini" type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" 
        :total="total" 
        :page.sync="listQuery.currPage" 
        :limit.sync="listQuery.pageSize"
        @pagination="getList"
      />
    <!-- 新增/编辑 对话框 -->
    <edit-role ref="addandupdrole" v-if="addandupdrole" :myVisible.sync="addandupdrole" @refreshDataList="getList"></edit-role>
  </div>
</template>
<script>
import Pagination from "@/components/Pagination";
import roleAddAndUpdate from "./edit_role";
import { pageRole,deleteRole} from "@/api/auth/role";
import { parseTime } from "@/utils";

export default {
   components: {
     Pagination,
    "edit-role": roleAddAndUpdate
  },
  data() {
    return {
      list: null,
      total: 0,
      listLoading: true,
      addandupdrole: false,
      listQuery: {
        currentPage: 1,
        pageSize: 10,
        roleName: undefined,
      }
    };
  },
  created() {
    this.getList();
  },
  watch:{
    addandupdrole(){
      console.log("父组件值发生改变："+this.addandupdrole)
    }
  },
  methods: {
    getList() {
      var that = this;
      that.listLoading = true;
      pageRole(self.listQuery).then(res => {
        that.list = res.data.list
        that.total = res.data.totalCount;
        that.listLoading = false;
      });
    },
    handleCreate(id) {
      this.addandupdrole = true;
      this.$nextTick(() => {
        this.$refs.addandupdrole.getResourceTreeList(id);
      });
    },
    handleDelete(id) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        let data = {}
        data.id = id
        deleteRole(data).then(res => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 2000
          })
          this.getList()
        })
      }).catch(() => {})
    }
  }
};
</script>

<style scoped>

</style>
